package com.evil.kingdom.task;

import com.evil.kingdom.listener.event.MillisecondTickEvent;
import com.evil.kingdom.listener.event.MinuteTickEvent;
import com.evil.kingdom.listener.event.SecondTickEvent;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 时钟-定时任务
 *
 * @author LY
 * @date 2024/5/1810:19
 */
@Component
@AllArgsConstructor
public class TickTask {

    private final ApplicationEventPublisher publisher;

    /**
     * 每100毫秒秒事件
     */
    @Scheduled(fixedRate = 100)
    public void millisecondTick() {
        publisher.publishEvent(new MillisecondTickEvent(LocalDateTime.now()));
    }

    /**
     * 每秒事件
     */
    @Scheduled(fixedRate = 1000)
    public void secondTick() {
        publisher.publishEvent(new SecondTickEvent(LocalDateTime.now()));
    }

    /**
     * 每分时间
     */
    @Scheduled(fixedRate = 60 * 1000)
    public void minuteTick() {
        publisher.publishEvent(new MinuteTickEvent(LocalDateTime.now()));
    }
}
